<?php
namespace app\api\model\Goods\Speci;

use app\api\model\Commodity;
use app\api\model\Goods;
use think\Model;

trait Speci
{

    /**
     * @param mixed $id
     * @return array|mixed|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id)
    {
        return self::where('pid', self::LEVEL)->find($id);
    }

    /**
     * @param $data
     * @return Unit
     */
    public function add($data)
    {
        $data['pid'] = self::LEVEL;
        return self::create($data);
    }

    /**
     * @param $where
     * @param $page
     * @param $size
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function index($where, $page =1, $size =10)
    {
        return self::where('pid', self::LEVEL)->where($where)->page($page, $size)->paginate($size);
    }

    public function getAllBrand()
    {
        return self::field('id,name,pid')->where('pid', self::LEVEL)->select();
    }

    /**
     * @param $id
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit($id, $data)
    {
        return self::where('pid', self::LEVEL)->where('id', $id)->update($data);
    }

    /**
     * @param $id
     * @return bool|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($id)
    {
        // 有绑定到商品则不能删除
        if ((Commodity::where(self::FIELD, $id)->count()) > 0) {
            return false;
        } else {
            return self::where('pid', self::LEVEL)->delete(['id' => $id]);
        }
    }
}
